<?php


namespace App\Http\Controllers\Weapp;


use App\Events\OrderRefund;
use App\Model\OrderGoods;
use App\Tool\Json;
use Illuminate\Support\Facades\DB;

class Refund extends Auth
{

    /**
     * 申请退款
     * Create by Peter
     * 2020/07/01 09:55:14
     * Email:904801074@qq.com
     * @return false|string
     * @throws \Exception
     */
    function create_refund()
    {

        try {

            \DB::beginTransaction();

            $user_id = resolve('weapp_user')['id'];

            $refund_data = \App\Model\Refund::where('order_goods_id', request()->input('order_goods_id'))
                ->where('user_id', $user_id)->first();


//            if ($refund_data) return Json::code(2, '请勿多次申请退款！');

            if($refund_data){

                if($refund_data->status==1) return Json::code(2, '该商品正在申请中，请等待商家审核');
                if($refund_data->status==2) return Json::code(2, '该商品正在退款中，三个工作日将返回到您的账户中');
                if($refund_data->status==3) return Json::code(2, '该商品已退款');


            }


            $order_info = OrderGoods::with('order.order_transaction')->find(request()->input('order_goods_id'));

            //支付方式
            $pay_type = $order_info->order->order_transaction->type;

            //交易号id
            $order_transaction_id = $order_info->order->order_transaction->id;

            $data = OrderGoods::with(['goods_sku' => function ($query) {

                //包括软删除的数据
                $query->withTrashed();

            }])->find(request()->input('order_goods_id'));


            $create_time = $data->created_at;

            $create_time = strtotime($create_time);


            $now = time();

            if ($now - $create_time > 60 * 60 * 24 * 30 * 3) return Json::code(2, '已超过最大的退款时间！');


            $info = \App\Model\Refund::create([
                'order_goods_id' => request()->input('order_goods_id'),
                'status' => 1,
                'name' => request()->input('name'),
                'phone' => request()->input('phone'),
                'reason' => request()->input('reason'),
                'user_id' => $user_id,
                'goods_id' => $data->goods_id,
                'goods_sku_id' => $data->goods_sku_id,
                'goods_name' => $data->goods_name,
                'goods_sku_msg' => $data->sku_msg,
                'goods_img' => $data->goods_sku->img,
                'goods_price' => $data->price,
                'num' => $data->num,
                'refund_price' => $data->price * $data->num,
                'pay_type' => $pay_type,
                'order_transaction_id' => $order_transaction_id,
                'order_id'=>$order_info->order->id

            ]);


            event(new OrderRefund($info));

            \DB::commit();

            return Json::code(1, 'success', $data);

        }catch (\Exception $exception){

            \DB::rollBack();

            return Json::code(2,$exception->getMessage());

        }
    }


    function order_detail()
    {

        $user_id = resolve('weapp_user')['id'];

        $id = request()->input('id');

        $data = \App\Model\Order::with(['order_goods.goods_sku' => function ($query) {
            //包括软删除的数据
            $query->withTrashed();

        }])->with('address')
            ->with('address_copy')
            ->with('store_copy')
            ->with('receipt_code')
            ->with('order_mention')
            ->where('id', $id)->where('user_id', $user_id)->first();

        $order_goods_list=$data->order_goods;


//        $order_goods_list->append('refund_status');

        foreach ($order_goods_list as $k=> $v){

            $v->append('refund_status');

            $order_goods_id=$v->id;

            $is_refund=\App\Model\Refund::where('order_goods_id', $order_goods_id)
                ->where('user_id', $user_id)->first();

            if($is_refund){
                $data->order_goods[$k]['is_refund']=true;
            }else{
                $data->order_goods[$k]['is_refund']=false;
            }
        }


        return Json::code(1, 'success', $data);

    }

    function refund_list(){

        $user_id = resolve('weapp_user')['id'];

        $list=\App\Model\Refund::orderBy('id','desc')->where('user_id',$user_id)->get();


        return Json::code(1,'success',$list);

    }


}
